
import java.lang.Exception;
import java.sql.*;
import java.util.*;

public class Categorie {

  private int id;
  private String nom;
  private static final String TABLE_CATEGORIE = "ap4_sp_categorie";
  private static bdd db = bdd.getInstance();

  // Constructeurs et créateur (dans la BDD)
  public Categorie(String nom) throws SQLException, IllegalArgumentException {
    if (Categorie.existCat(nom)) {
      throw new IllegalArgumentException("La catégorie existe déjà");
    } else {
      this.nom = nom;
      this.createCategorie();
    }
  }

  public Categorie(int id, String nom) {
    this.id = id;
    this.nom = nom;
  }

  private void createCategorie() throws SQLException {
    try {
      db.update("INSERT INTO " + TABLE_CATEGORIE + "(nom) VALUES ('" + this.nom + "')");
      ResultSet r = db.req("SELECT idCategorie FROM " + TABLE_CATEGORIE + " WHERE nom='" + this.nom + "'");
      r.next();
      this.id = r.getInt("idCategorie");
    } catch (SQLException e) {
      throw e;
    }
  }

  // Accesseurs
  public String getNom() {
    return this.nom;
  }

  public int getId() {
    return this.id;
  }

  public void setNom(String pNom) throws IllegalArgumentException, SQLException {
    if (pNom.length() < 50) {
      try {
        db.update("UPDATE " + TABLE_CATEGORIE + " SET nom='" + pNom + "' WHERE nom='" + this.nom + "'");
        this.nom = pNom;
      } catch (SQLException e) {
        throw e;
      }
    } else {
      throw new IllegalArgumentException("Le nom spécifié est trop long.");
    }
  }

  // Méthodes
  @Override
  public boolean equals(Object o) {
    if (o.getClass() == this.getClass()) {
      if (((Categorie) o).getNom().equals(this.nom)) {
        return true;
      }
    }
    return false;
  }

  @Override
  public String toString() {
    return "Catégorie " + this.nom + "[" + this.id + "]";
  }

  public boolean exists() throws SQLException {
    ResultSet resultat;
    try {
      resultat = db.req("SELECT * FROM " + TABLE_CATEGORIE + " WHERE nom = '" + this.nom + "'");
      return resultat.first();
    } catch (SQLException e) {
      throw e;
    }
  }

  public static Categorie getCategorieNom(String nom) throws SQLException {
    ArrayList<Categorie> categories = null;
    try {
      categories = Categorie.chargerCategories();
    } catch (SQLException e) {
      throw e;
    }
    for (Categorie cate : categories) {
      if (cate.getNom().equals(nom)) {
        return cate;
      }
    }
    return null;
  }

  public static ArrayList<Categorie> chargerCategories() throws SQLException {
    ArrayList<Categorie> categories = new ArrayList<Categorie>();
    try {
      ResultSet res = db.req("SELECT * FROM " + TABLE_CATEGORIE + " ORDER BY nom ASC");
      while (res.next()) {
        categories.add(new Categorie(res.getInt("idCategorie"), res.getString("nom")));
      }
    } catch (SQLException e) {
      throw e;
    }
    return categories;
  }

  public static Categorie getCategorie(int id, ArrayList<Categorie> categories) {
    for (Categorie cate : categories) {
      if (cate.getId() == id) {
        return cate;
      }
    }
    return null;
  }

  public static Categorie getCategorie(String nom) throws SQLException {
    ArrayList<Categorie> cats;
    try {
      cats = chargerCategories();
    } catch (SQLException e) {
      throw e;
    }
    for (Categorie c : cats) {
      if (c.getNom().equals(nom)) {
        return c;
      }
    }
    return null;
  }

  public static boolean existCat(String nom) throws SQLException {
    try {
      bdd db = bdd.getInstance();
      ResultSet res = db.req("SELECT * FROM " + TABLE_CATEGORIE + " WHERE nom ='" + nom + "'");
      return res.first();
    } catch (SQLException e) {
      throw e;
    }
  }

  public void delete() throws SQLException {
    try {
      db.update("DELETE FROM " + TABLE_CATEGORIE + " WHERE idCategorie=" + this.id);
    } catch (SQLException e) {
      throw e;
    }
  }
}